package com.vitabuybuy.tv.ui.shopcenter.store

import android.os.Bundle
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.library.common.base.BaseFragment
import com.library.common.base.MessageModel
import com.library.common.dto.MessageEvent
import com.library.common.ext.mmkv
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.FragmentStoreHomeBinding
import com.vitabuybuy.tv.dto.StroeDetailDto
import com.vitabuybuy.tv.dto.TabEntity
import com.library.common.toast.XToast
import com.library.common.util.FragmentUtils
import com.library.common.util.MMKVContancts
import com.vitabuybuy.tv.ui.auth.LoginActivity
import com.vitabuybuy.tv.ui.shopcenter.SearchGoodsOrStoreActivity
import com.vitabuybuy.tv.ui.shopcenter.store.viewmodel.StoreHomeViewModel
import com.vitabuybuy.tv.utils.EventCode
import kotlinx.android.synthetic.main.fragment_store_home.*
import java.util.*

class StoreHomeFragment : BaseFragment<StoreHomeViewModel, FragmentStoreHomeBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_store_home

    private var storeId :Int?= -1

    private val mTabEntities = ArrayList<CustomTabEntity>()
    override fun initView(savedInstanceState: Bundle?) {
        mBinding.mViewModel = viewModel
        val list = arrayListOf(
            getString(R.string.new_product),
            getString(R.string.group_buy),
            getString(R.string.bidding),
            getString(R.string.get_coupons_1),
            getString(R.string.live)
        )
        for (str in list) {
            mTabEntities.add(TabEntity(str, 0, 0))
        }
        mTabLayout_1.setTabData(mTabEntities)
        storeId = (activity as StoreHomeActivity).getStoreId()
    }

    override fun lazyLoadData() {
        viewModel.getStoreDetail(storeId)
    }

    override fun observeData() {
        viewModel.storeDtail.observe(this, androidx.lifecycle.Observer {
            when( (activity as StoreHomeActivity).getJumpType()){
                StoreHomeActivity.JUMP_BIDING_PAGE->{
                    mTabLayout_1.currentTab = 2
                }
                StoreHomeActivity.JUMP_GROUP_PAGE->{
                    mTabLayout_1.currentTab = 1
                }
                else->{
                    mTabLayout_1.currentTab = 0
                }
            }
            showCurrentFragment(mTabLayout_1.currentTab)
        })
    }

    private fun showCurrentFragment(index: Int) {
        when (index) {
            0 -> FragmentUtils.replaceFragment(
                childFragmentManager, R.id.containerFragment,
                StoreHomeNewFragment::class.java.name
            )
            1 -> FragmentUtils.replaceFragment(
                childFragmentManager, R.id.containerFragment,
                StoreHomeGroupBuyFragment::class.java.name
            )
            2 -> FragmentUtils.replaceFragment(
                childFragmentManager, R.id.containerFragment,
                StoreHomeBiddingFragment::class.java.name
            )
            3 -> FragmentUtils.replaceFragment(
                childFragmentManager, R.id.containerFragment,
                StoreHomeGetCouponsFragment::class.java.name
            )
            4 -> FragmentUtils.replaceFragment(
                childFragmentManager, R.id.containerFragment,
                StoreHomeLiveFragment::class.java.name
            )
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                viewModel.storeDtail.value?.isAttention = !(msg.obj as Boolean)
                if (viewModel.storeDtail.value!!.isAttention) {
                    viewModel.storeDtail.value?.favoriteQuantity = viewModel.storeDtail.value?.favoriteQuantity!! + 1
                    XToast.success(getString(R.string.already_success_follow))
                } else {
                    viewModel.storeDtail.value?.favoriteQuantity = viewModel.storeDtail.value?.favoriteQuantity!! - 1
                    XToast.success(getString(R.string.cancel_collect_follow))
                }
            }
        }
    }

    fun getStoreDetail(): StroeDetailDto {
        return viewModel.storeDtail.value!!
    }

    fun getStoreId(): Int? {
        return storeId
    }

    override fun eventHandling() {
        ivSearch.setOnClickListener {
            SearchGoodsOrStoreActivity.openActivity(mContext,storeId)
        }
        mTabLayout_1.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                showCurrentFragment(position)
            }
            override fun onTabReselect(position: Int) {
            }
        })
        btnFollowShop.setOnClickListener {
            if (!mmkv.decodeBool(MMKVContancts.IS_LOGIN_IN, false)) {
                LoginActivity.openActivity(mContext)
                return@setOnClickListener
            }
            viewModel.collectOrcancel(
                storeId,
                viewModel.storeDtail.value?.isAttention
            )
        }
    }

    override fun onEvent(event: MessageEvent) {
        when(event.code){
            EventCode.update_attantion->{
                viewModel.storeDtail.value?.isAttention = event.message as Boolean
                if (viewModel.storeDtail.value!!.isAttention) {
                    viewModel.storeDtail.value?.favoriteQuantity = viewModel.storeDtail.value?.favoriteQuantity!! + 1
                    XToast.success(getString(R.string.already_success_follow))
                } else {
                    viewModel.storeDtail.value?.favoriteQuantity = viewModel.storeDtail.value?.favoriteQuantity!! - 1
                    XToast.success(getString(R.string.cancel_collect_follow))
                }
            }
        }
    }
}
